package com.liu.interceptor;

import org.springframework.util.Base64Utils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器
 */

public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        //获取请求头，授权参数值
        String auth = request.getHeader("Authorization");
        if(null==auth){
            //无权访问
            return false;
        }
        //传了参数
        String[] tmp = auth.split("\\s+");
        //获取加密后的字符串
        String info = tmp[1];
        info = new String(Base64Utils.decode(info.getBytes()));
        tmp = info.split(":");
        if("admin".equals(tmp[0])&&"1357".equals(tmp[1]))
            return true;//放行
        return false;
    }
}
